package mylittleinvestigations;

import mylittleinvestigations.CaseInformation.CaseXmlWriter;
import mylittleinvestigations.CaseInformation.Case;
import mylittleinvestigations.CaseInformation.CaseXmlReader;
import mylittleinvestigations.CaseInformation.CaseParseException;
import mylittleinvestigations.GameUi.Tab;
import mylittleinvestigations.GameUi.TabRow;
import java.io.IOException;
import java.util.*;
import org.newdawn.slick.*;

public class ZoomedView
{
    private String id;
    private Sprite backgroundSprite;
    private ArrayList<ForegroundElement> foregroundElementList;

    private ForegroundElement currentInteractiveForegroundElement;

    private Tab exitTab;

    public ZoomedView(String id)
    {
        this.id = id;
        this.foregroundElementList = new ArrayList<ForegroundElement>();

        this.exitTab = new Tab((int)Settings.DisplayBounds.getX() - Tab.Width, true /* isClickable */, "EXIT", true /* useCancelClickSoundEffect */, TabRow.Bottom);
    }
    
    private String backgroundTextureId;
    
    public String getBackgroundTextureId() { return this.backgroundTextureId; }
    public void setBackgroundTextureId(String backgroundTextureId) { this.backgroundTextureId = backgroundTextureId; }

    private Sprite getBackgroundSprite()
    {
        if (this.backgroundSprite == null)
        {
            this.backgroundSprite = Case.getInstance().getSpriteManager().GetSpriteFromId(this.getBackgroundTextureId());
        }

        return this.backgroundSprite;
    }
    
    private boolean isFinished;
    
    public boolean getIsFinished() { return this.isFinished; }
    public void setIsFinished(boolean isFinished) { this.isFinished = isFinished; }

    public void AddForegroundElement(ForegroundElement element)
    {
        this.foregroundElementList.add(element);
    }

    public void Begin()
    {
        this.setIsFinished(false);
    }

    public void Update(GameContainer gc, int delta) throws CaseParseException
    {
        if (this.currentInteractiveForegroundElement != null)
        {
            this.currentInteractiveForegroundElement.UpdateInteraction(gc, delta);

            if (this.currentInteractiveForegroundElement.getIsInteractionFinished())
            {
                this.currentInteractiveForegroundElement = null;
            }
            else
            {
                return;
            }
        }

        for (int i = 0; i < this.foregroundElementList.size(); i++)
        {
            this.foregroundElementList.get(i).Update(gc, delta);
        }
        
        for (int i = 0; i < this.foregroundElementList.size(); i++)
        {
            ForegroundElement foregroundElement = this.foregroundElementList.get(i);
            
            if (foregroundElement.getIsClicked() && foregroundElement.getIsInteractive())
            {
                foregroundElement.BeginInteraction();
                this.currentInteractiveForegroundElement = foregroundElement;
                return;
            }
        }

        this.exitTab.Update();

        if (this.exitTab.getIsClicked())
        {
            this.setIsFinished(true);
        }
    }

    public void Draw(GameContainer gc, Graphics g) throws SlickException
    {
        if (this.currentInteractiveForegroundElement == null || !this.currentInteractiveForegroundElement.getIsInteractionFullScreen())
        {
            this.getBackgroundSprite().Draw(gc, g, new Vector2(0, 0));

            for (int i = 0; i < this.foregroundElementList.size(); i++)
            {
                this.foregroundElementList.get(i).Draw(gc, g);
            }

            this.exitTab.Draw(gc, g);
        }

        if (this.currentInteractiveForegroundElement != null)
        {
            this.currentInteractiveForegroundElement.DrawInteraction(gc, g);
            return;
        }
    }

    public void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("ZoomedView");
        writer.WriteTextElement("Id", id);
        writer.WriteSpriteId("BackgroundSpriteId", this.backgroundTextureId);
        
        writer.StartElement("ForegroundElementList");
        
        for (int i = 0; i < this.foregroundElementList.size(); i++)
        {
            writer.StartElement("Entry");
            this.foregroundElementList.get(i).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.EndElement();
    }

    public ZoomedView(CaseXmlReader reader)
    {
        this.foregroundElementList = new ArrayList<ForegroundElement>();

        this.exitTab = new Tab((int)Settings.DisplayBounds.getX() - Tab.Width, true /* isClickable */, "EXIT", true /* useCancelClickSoundEffect */, TabRow.Bottom);
        
        reader.StartElement("ZoomedView");
        this.id = reader.ReadTextElement("Id");
        this.backgroundTextureId = reader.ReadTextElement("BackgroundSpriteId");
        
        reader.StartElement("ForegroundElementList");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            this.foregroundElementList.add(new ForegroundElement(reader));
        }
        
        reader.EndElement();
        
        reader.EndElement();
    }

    public void markChildSprites(String markParent) throws CaseParseException
    {
        this.getBackgroundSprite().markChildSprites(markParent);
        
        for (ForegroundElement fe : this.foregroundElementList)
        {
            fe.markChildSprites(markParent);
        }
    }
}